//
//  MyInvoiceListViewController.m
//  GLGW_Shoping
//
//  Created by whx on 2018/12/10.
//  Copyright © 2018年 yang. All rights reserved.
//

#import "MyInvoiceListViewController.h"
#import "InvoiceDetailViewController.h"
#import "MyInvoiceViewModel.h"
#import "MyInvoiceModel.h"

@interface MyInvoiceListViewController ()
{
    MyInvoiceModel *invoiceModel;
}
@property (nonatomic, strong) MyInvoiceViewModel *viewModel;
@end

@implementation MyInvoiceListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.viewModel = [[MyInvoiceViewModel alloc] initWithViewToViewModel:self.tableView theSuperView:self.view];
    self.viewModel.type = self.type;
    kWeakSelf(self);
    self.viewModel.BlockWithDrawlDetail = ^(MyInvoiceListDetailModel *model) {
        [weakself didSele:model];
    };
    [self headerRereshing];
}

- (void)didSele:(MyInvoiceListDetailModel *)model
{
    InvoiceDetailViewController *vc = [InvoiceDetailViewController new];
    vc.type = self.type;
    vc.deModel = model;
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)headerRereshing
{
    self.page = 1;
    [self loadData];
}

- (void)footerRereshing
{
    if (self.page < invoiceModel.data.total) {
        self.page ++;
        [self loadData];
    }else{
        [GLGWProgressHUD showNotice:@"没有更多服务的买家了！" view:nil];
        [self.tableView.mj_footer endRefreshing];
        [self.tableView.mj_header endRefreshing];
    }
}

- (void)loadData
{
    NSMutableDictionary *dic = [@{
                          @"pageNo" : @(self.page),
                          @"pageSize" : @(20)    // 默认也是20条
                          } mutableCopy];
    @weakify(self)
    [[self.viewModel.invoiceCommand execute:dic] subscribeNext:^(id  _Nullable x) {
        @strongify(self)
        self->invoiceModel = (MyInvoiceModel *)x;
        [self.tableView.mj_footer endRefreshing];
        [self.tableView.mj_header endRefreshing];
    } error:^(NSError * _Nullable error) {
        [self.tableView.mj_footer endRefreshing];
        [self.tableView.mj_header endRefreshing];
    }];
}


@end
